package com.l.web.master.system.menu;

import com.l.web.ConstantConfigure;
import org.apache.log4j.Logger;
import org.l.qiao.*;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.search.Sort;
import org.l.qiao.web.ControllerSupport;
import org.l.qiao.web.KeyUpdate;
import org.l.qiao.web.ResponseCode;
import org.l.qiao.web.ResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(MenuController.URI)
public class MenuController extends ControllerSupport {
    public static final String URI = LConstant.MASTER_URI + "/system/menu";

    private static final Logger LOGGER = Logger.getLogger(MenuService.class);
    @Autowired
    private MenuService menuService;

    @Privilege(module = "系统", function = "菜单管理", operation = "查询")
    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put(LConstant.CONTROLLER_KEY, URI);
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "查询")
    @RequestMapping("tree")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Sort sort = new Sort();
        sort.asc(Menu.INDEX);
        List menus = menuService.getList(sort);
        modelMap.put("menus", menus);
        return new ModelAndView(URI + "/tree", modelMap);
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "详情")
    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        String id = form.getString("id");
        LOGGER.debug("查看菜单信息：id=" + id);
        if (Validate.check(Validate.OBJECTID, id)) {
            Cluster menu = menuService.detail(id, modelMap);

            if (menu.isEmpty()) {
                LOGGER.error("所查看的菜单不存在");
                response.sendError(404);
                return null;
            }

            List parents = menuService.findParents(menu);

            modelMap.put("menu", menu);
            modelMap.put("check", ClusterUtil.getCheck(Menu.class));
            modelMap.put("parents", parents);
        } else {
            LOGGER.error("非法查看菜单信息：id=" + id);
            response.sendError(404);
            return null;
        }

        return new ModelAndView(URI + "/detail", modelMap);
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "添加")
    @RequestMapping("add")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        getConfigure(ConstantConfigure.CONFIGURE_WEB_NODE);
        List menus = menuService.getList(null);
        modelMap.put("parentId", request.getParameter("parentId"));
        modelMap.put("menus", menus);
        modelMap.put("check", ClusterUtil.getCheck(Menu.class));
        return new ModelAndView(URI + "/add", modelMap);
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "添加")
    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        Menu menu = new Menu();
        boolean result = form.getCluster(menu);
        LOGGER.debug("添加菜单：" + menu);

        if (result) {
            int code = menuService.save(menu);
            ResponseInfo responseInfo = new ResponseInfo(code);

            if (code == ResponseCode.FINISH) {
                responseInfo.put("id", menu.getId());
            }

            out(response, responseInfo);
            out(response, responseInfo);
        } else {
            response.sendError(404);
            LOGGER.error("非法添加菜单：" + menu);
        }
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "修改")
    @RequestMapping("update")
    public void update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        KeyUpdate keyUpdate = form.getKeyUpdate(Menu.class);
        LOGGER.debug("修改信息信息：" + keyUpdate);
        if (keyUpdate.check()) {
            int code = ResponseCode.FAILURE;

            try {
                code = menuService.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
            }

            out(response, code);
        } else {
            LOGGER.error("非法修改：" + keyUpdate);
            response.sendError(404);
        }
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "修改")
    @RequestMapping("parent")
    public ModelAndView parent(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        String id = request.getParameter("id");
        boolean result = Validate.check(Validate.OBJECTID, id);
        if (result) {
            List menus = menuService.parent(id);
            modelMap.put("menus", menus);
        } else {
            LOGGER.error("非法查询菜单树：id=" + id);
            response.sendError(404);
            return null;
        }
        return new ModelAndView(URI + "/parent", modelMap);
    }

    @Privilege(module = "系统", function = "菜单管理", operation = "删除")
    @RequestMapping("remove")
    public void remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = getForm(request);
        List<String> ids = form.getList("id");
        LOGGER.debug("删除菜单：id=" + ids);

        if (Validate.check(Validate.OBJECTID, ids)) {
            int code = menuService.remove(ids);
            out(response, code);
        } else {
            LOGGER.error("非法删除菜单：id=" + ids);
            response.sendError(404);
        }
    }
}

